/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBean;

import entities.Article;
import entities.Bid;
import entities.Category;
import entities.UserProject;
import interfaces.ArticleInterface;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author Thibault
 */
@ManagedBean
@ViewScoped
public class SearchArticle implements Serializable {

    @EJB
    private ArticleInterface articleEJB;
    private List<String> selectedCategories;
    private List<Article> searchResult;
    private String keyWord;
    private Article selectedArticle;
    private double newBid;

    public SearchArticle() {
        searchResult = new ArrayList<Article>();
        selectedCategories = new ArrayList<String>();
        selectedCategories.add("All");
    }

    public void articleSelected() {
        newBid = selectedArticle.getActualPrice() + 1;
    }

    public void search() {
        searchResult.clear();
        selectedArticle = null;
        if (selectedCategories.isEmpty()) {
            searchResult = articleEJB.searchByName(keyWord);
        } else {
            List<Category> categories = new ArrayList<Category>();
            for (String str : selectedCategories) {
                categories.add(Category.valueOf(str));
            }
            searchResult = articleEJB.searchByNameAndCategories(keyWord, categories);
        }
        FacesMessage msg = new FacesMessage(searchResult.size() + " results found.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public void createNewBid() {
        UserProject loggedUser = ((HeaderBean) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("headerBean")).getLoggedUser();
        Bid bid = new Bid(loggedUser, selectedArticle, new Date(), newBid);
        selectedArticle.getBiddingHistory().add(bid);
        articleEJB.updateArticle(selectedArticle);
    }

    public List<String> getSelectedCategories() {
        return selectedCategories;
    }

    public void setSelectedCategories(List<String> selectedCategories) {
        this.selectedCategories = selectedCategories;
    }

    public String getKeyWord() {
        return keyWord;
    }

    public void setKeyWord(String keyWord) {
        this.keyWord = keyWord;
    }

    public List<Article> getSearchResult() {
        return searchResult;
    }

    public Article getSelectedArticle() {
        return selectedArticle;
    }

    public void setSelectedArticle(Article selectedArticle) {
        this.selectedArticle = selectedArticle;
    }

    public double getNewBid() {
        return newBid;
    }

    public void setNewBid(double newBid) {
        this.newBid = newBid;
    }
}
